package com.sea.supply.controller;


import com.codecrane.core.controller.UploaderController;
import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.physical.service.*;

import com.sea.supply.entity.Buy;
import com.sea.supply.entity.PdfFile;
import com.sea.supply.entity.PdfRead;
import com.sea.supply.service.PdfFileService;
import com.sea.supply.service.PdfService;
import com.sea.supply.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *  设计部控制器
 * @author 陈鸿飞
 *  <br/>
 *  <br/>
 */
@Controller
@RequestMapping("/sea/pdf")
public class PdfController {
    @Autowired
    private PdfService pdfService;
    @Autowired
    private PdfFileService pdfFileService;

    @ResponseBody
    @RequestMapping("/findByPdfPlanNo")
    public AjaxTableReturn findByPdfPlanNo(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                           PdfFile pdfFile) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<PdfRead> list = pdfService.findByPdfPlanNo(pdfFile);
        PageInfo<PdfRead> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     String createPlanNo) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<PdfFile> list = pdfFileService.findByCreatePlanNo(createPlanNo);
        PageInfo<PdfFile> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    @RequestMapping("/main")
    public String main(Model model) {
        return "/sea/supply/pdf/main";
    }

    @RequestMapping("/record")
    public String main(Model model,@RequestParam("createPlanNo")String createPlanNo) {
        model.addAttribute("createPlanNo",createPlanNo);
        return "/sea/supply/pdf/record";
    }

    @RequestMapping("/back")
    public String back(Model model) {

        return "/sea/supply/pdf/back";
    }

    @RequestMapping("/editPage")
    public String editPage(Model model,@RequestParam("ids")String ids) {
        model.addAttribute("ids",ids);
        return "/sea/supply/pdf/edit";
    }

    @RequestMapping("/importPdfSku")
    @ResponseBody
    public AjaxReturn importAutoDistribute(Model model,
                                           MultipartFile importSession,
                                           @RequestParam("createPlanNo")String createPlanNo){
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            if (null == importSession || importSession.isEmpty()) {
                return result.setMsg("请上传文件！！！");
            } else {
                String fileName = importSession.getOriginalFilename();
                if (!(fileName.contains(".pdf"))) {
                    return result.setMsg("只能上传pdf文件！！！");
                }
                pdfService.importPdfSku(importSession,fileName,createPlanNo);
                result.setOk(true);
                result.setMsg("上传成功！！");
            }
        } catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        }catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统处理异常,请联系管理员");
            return result;
        }
        return result;
    }


    @RequestMapping("/returnBack")
    @ResponseBody
    public AjaxReturn returnBack(Model model, @RequestParam("pdfPlanNo")String pdfPlanNo) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            pdfService.deleteByPdfPlanNo(pdfPlanNo);
            result.setData(pdfPlanNo).setOk(true).setMsg("上传成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统处理异常,请联系管理员");
            return result;
        }
        return result;
    }
}



